class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
}

/**
 * 将有序数组转换为二叉搜索树，二分查找思路即可
 */

public class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        if(nums == null || nums.length == 0) {
            return null;
        }
        return createTree(nums, 0, nums.length - 1);
    }

    private TreeNode createTree(int[] nums, int left, int right) {
        if(left > right) {
            return null;
        }
        int index = (right - left) / 2 + left;
        TreeNode root = new TreeNode(nums[index]);
        root.left = createTree(nums, left, index - 1);
        root.right = createTree(nums, index + 1, right);
        return root;
    }
}